<?php
class Navs extends MY_Controller{
	var $beforeFilter = array(
			'initAdmin' => array(
				'only' => array('admin','add','remove','edit','save'),
			),
		);

	function __construct()
	{
		parent::MY_Controller();
		$this->load->helper(array('url','system','form'));
		$this->load->model('Nav');
	}
	
	function index()
	{
		//��ҳ�滺��5����
		//$this->output->cache(5);
		$parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']):null;
		$where = array();
		if(isset($_GET['position'])) $where['position'] = $_GET['position']; 
		$result = $this->Nav->listNav($parent_id,$where);
		$this->assign['result'] = cat_options(0, $result);
		exit();
		$this->display();
	}

	function view($id=null){

	}

	function admin(){
		$result = $this->Nav->listNav();
		$this->assign['result'] = cat_options(0, $result);
		$this->display();
	}

	function save(){
		$redirect = $this->getRedirect('/index.php/navs/admin');
		$rules['name']	= "trim|required";
		$rules['url']	= "trim|required";
		if ($this->validation($rules) == FALSE)
		{
			$this->showMessage('error_wrong_input'); 
		}
		$this->Nav->save($_POST);
		$this->showMessage('mes_success',$redirect);
	}

	function doEdit($id=null){
		$redirect = $this->getRedirect('nav_admin');
		$result = $this->Nav->listNav();
		if($id>0){
			$nav  = getByCond($result,array('id'=>$id));
			$nav  = $nav[0];
			$this->assign['result'] = $nav;
		}
		$options = cat_list(cat_options(0, $result),0, $nav['parent_id'],true, 3,true);
		$this->assign['options'] = $options;
		$this->assign['redirect'] = $redirect;
		$this->template = 'navs_save';
		$this->display();
	}
	
	function add(){
		$this->doEdit();
	}

	function edit($id){
		$this->doEdit($id);
	}

	function remove(){
		$redirect = $this->getRedirect('/index.php/navs/admin');
		$id = empty($id) ? $_REQUEST['id'] : $id;
		$this->Nav->removeByPks($id);
		$this->showMessage('mes_success',$redirect);
	}
}